$(function () {
    $("#form_1").bootstrapValidator({
        feedbackIcons: {

            //校验通过时的输入框状态
            valid: 'glyphicon glyphicon-ok',
            //校验失败时的输入框状态
            invalid: 'glyphicon glyphicon-remove',
            //校验中的输入框状态
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                message: '用户名有误',
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        min: 1,
                        max: 10
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    stringLength: {
                        min: 3,
                        max: 12
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {
                        message: '手机不能为空'
                    },
                    regexp: {
                        regexp: '^1[0-9]{10}$',
                        message: '手机号长度错误'
                    }
                }
            }

        }
    });
    $("#register").click(function () {
        alert( $("#form_1").data("bootstrapValidator").isValid());
        if ( $("#form_1").data("bootstrapValidator").isValid()) {
            myPostAjax(
              "/catering_system_app/user/addUser",
              {
                  userName:$("#username").val(),
                  password:$("#password").val(),
                  sex:$("input[name='sex']:checked").val(),
                  phone:$("#phone").val()
               },
                register
          )
        }

    });
    $("#username").blur(function () {
        $("span:eq(0)").text("");
        myGetAjax("/catering_system_app/user/checkUser",
            {
                userName:$("#username").val()
            },
            checkUserByUserName
            );
    })
});
function checkUserByUserName(RespVo) {
    if (RespVo.code=="200"){
        $("span:eq(0)").attr("style", "color:green");
        $("span:eq(0)").text("用户名可用");
    } else if (RespVo.code=="100"){
        $("span:eq(0)").attr("style", "color:red");
        $("span:eq(0)").text("用户名已存在");
    }
}
function register(RespVo) {
    alert(RespVo);
    if (RespVo.code == "200") {
        alert("注册成功");
        location.href = "index.html"
    } else if (RespVo.code == "100") {
        alert("注册失败")
    } else {
        alert("发生其他错误")
    }
}